    /**
     * @author sahsharm
     */

    var CourseMgtController = (function () {


    	var registeredHelpers = false;
    	var i = 1;

    	function registerHelpers() {
    		if (registeredHelpers == true)
    			return;
    		registeredHelpers = true;
    		Handlebars.registerHelper("generateNavId", function (data) {
    			var id = "course-tab-" + i++;
    			return "id=" + id;
    		});

    		Handlebars.registerHelper("generateHref", function (data) {
    			var href = "#course-content-" + (i - 1);
    			return "href=" + href;
    		});

    		Handlebars.registerHelper("generateContentId", function (data) {
    			var id = "course-content-" + (i - 1);
    			return "id=" + id;
    		});

    		Handlebars.registerHelper("generateCourseDetailDivId", function (data) {
    			var id = "course-tree-div-" + (i - 1);
    			return "id=" + id;
    		});

    	}



    	searchCourseOfferings = function () {
    		CourseMgtService.searchCourseOfferings("CAT", searchCourseOfferings_Result);
    	}
    	getPublicCourseOfferings = function () {
    		CourseMgtService.getPublicallyAvailableCourseOfferings(getPublicCourseOfferings_Result)
    	};

    	getCourseOfferingDetails = function (courseOfferingCode) {
    		CourseMgtService.getCourseOfferingDetails(courseOfferingCode, getCourseOfferingDetails_Result);
    	};


    	getCourseOfferingLessonDetails = function (courseOfferingCode, lessonCode, viewContainer) {
    		alert("get details");
    		CourseMgtService.getLesson(courseOfferingCode, lessonCode, getCourseOfferingLessonDetails_Result, viewContainer);
    	}

    	function getPublicCourseOfferings_Result(data) {
    		var source = $("#courseoffering-info-tmpl").html();
    		var template = Handlebars.compile(source);
    		$("#courseoffering-list").append(template(data));

    		//Rating
    		$('.co-rating').raty({
    			score: function () {
    				return $(this).attr('data-rating');
    			},
    			numberMax: 5,
    			starOff: "../../assets/img/star-off.png",
    			starOn: "../../assets/img/star-on.png",
    			starHalf: "../../assets/img/star-half.png",
    			halfShow: true,
    			width: 150,
    		});

    		$('.description').dotdotdot({

    		});

    	};

    	function getCourseOfferingDetails_Result(data) {
    		openCourseOfferingDetails(data);
    	};

    	function searchCourseOfferings_Result(data) {
    		var source = $("#courseoffering-search-tmpl").html();
    		var template = Handlebars.compile(source);
    		$("#results").append(template(data));
    	}

    	function getCourseOfferingLessonDetails_Result(data, view) {
    		var lessonContainer = view;
    		var temp = $(lessonContainer).find(".info-title")[0];
    		var prevHTML = $(temp).html();
    		var newHTML = prevHTML + " " + data.name;
    		$(temp).html(newHTML);

    		var temp1 = $(lessonContainer).find(".lesson-units .content").eq(0);
    		temp1.append(data.description);
    	}

    	function openCourseOfferingDetails(data) {
    		var source = $("#courseofferings-nav-tmpl").html();
    		var templ = Handlebars.compile(source);

    		//Add a new tab for the course offering being viewed
    		$("#courseofferings-nav").append(templ(data));

    		//Add a blank container in which the new course offering page will be loaded
    		source = $("#courseoffering-content-tab-tmpl").html();
    		templ = Handlebars.compile(source);
    		$("#content-holder").append(templ);

    		var sel = "#course-tab-" + (i - 1) + " a[href='#course-content-" + (i - 1) + "']";

    		//Load the page in the above container
    		$("#course-content-" + (i - 1)).load("../course/courseoffering_detail.html", function () {
    			//Register the partial for recurisve displaying of lessons and units
    			Handlebars.registerPartial("units-recursion", $("#units-recursion-partial").html());

    			//Get the template for displaying of course structure , this template uses the partial registered above.
    			source = $("#courseoffering-details-nav-tmpl").html();
    			templ = Handlebars.compile(source);

    			source = $("#courseoffering-details-nav-holder-tmpl").html();
    			var templ1 = Handlebars.compile(source);

    			//Append the holder for course structure in tree view div
    			$(this).find("#treeViewDiv").append(templ1);

    			//Add the navigation list to the holder and convert to jstree
    			$("#course-tree-div-" + (i - 1)).append(templ(data));

    			//Bind the selected event on the tree and open the relevant node details in the main section
    			$("#course-tree-div-" + (i - 1)).bind("select_node.jstree", function (event, data) {
    				var name = data.rslt.obj.attr("data-name");
    				var type = data.rslt.obj.attr("data-type");
    				var contentHolder = $(event.target).parent().parent().siblings(".current-content");
    				if (type == "LESSON") {
    					getLessonInfo(data.code, name, contentHolder);
    				} else if (type == "UNIT") {
    					var parentLessonName = $(data.rslt.obj).closest("li[data-type='LESSON']").attr('data-name');
    					getUnitInfo(data.code, parentLessonName, name, contentHolder);
    				}
    			})

    			$(this).find("#co_descr").append(data.description);

    			$("#course-tree-div-" + (i - 1)).jstree({
    				"themes": {
    					"theme": "apple",
    					"url": "../../lib/jstree/themes/apple/style.css",
    					"dots": false,
    					"icons": true
    				},

    				"core": {
    					"animation": 100,
    				},

    				"ui": {
    					"select_limit": 1,
    					"selected_parent_close": "select_parent"
    				},
    				"plugins": ["themes", "html_data", "ui"]
    			});


    			$(this).find('#co_rating').raty({
    				score: function () {
    					alert(data);
    					return data.rating;
    				},
    				numberMax: 5,
    				starOff: "../../assets/img/star-off.png",
    				starOn: "../../assets/img/star-on.png",
    				starHalf: "../../assets/img/star-half.png",
    				halfShow: true,
    				width: 150,
    			});

    		});

    	}

    	function getLessonInfo(courseOfferingCode, lessonName, contentHolder) {
    		contentHolder.children().remove();
    		contentHolder.load("../course/lesson_detail.html", function () {
    			CourseMgtController.getCourseOfferingLessonDetails(courseOfferingCode, lessonName, contentHolder);
    		});
    	}

    	function getUnitInfo(courseOfferingCode, lessonName, unitName, contentHolder) {

    	}


    	return {
    		registerHelpers: registerHelpers,
    		searchCourseOfferings: searchCourseOfferings,
    		searchCourseOfferings_Result: searchCourseOfferings_Result,
    		getPublicCourseOfferings: getPublicCourseOfferings,
    		getCourseOfferingDetails: getCourseOfferingDetails,
    		getCourseOfferingDetails_Result: getCourseOfferingDetails_Result,
    		getCourseOfferingLessonDetails: getCourseOfferingLessonDetails,
    		getCourseOfferingLessonDetails_Result: getCourseOfferingLessonDetails_Result,
    		getPublicCourseOfferings_Result: getPublicCourseOfferings_Result,
    	};

    })();